package cave.utils;

import java.util.ArrayList;
import android.util.Log;
import cave.game.InputEvent;
import cave.renderer.Sprite;

public class Pool {

	private static ArrayList<InputEvent> freeInputEvents;
	private static ArrayList<Sprite> freeSprites;
	
	private Pool() {}
	
	static {
		freeInputEvents = new ArrayList<InputEvent>();
		freeSprites = new ArrayList<Sprite>();
		
		for (int i = 0; i < 20; ++i) {
			freeInputEvents.add(new InputEvent());
			freeSprites.add(new Sprite());
		}
	}

	public static InputEvent getInputEvent() {
		if(freeInputEvents.size() != 0) {
			return freeInputEvents.remove(freeInputEvents.size() - 1);
		} else {
			return new InputEvent();
		}
	}
	
	public static void free(InputEvent inputEvent) {
		if(inputEvent != null) {
			freeInputEvents.add(inputEvent);
		} else {
			Log.w("Cave", "NULL was passed to free(InputEvent)");
		}
	}
	
	public static Sprite getSprite() {
		if(freeSprites.size() != 0) {
			return freeSprites.remove(freeSprites.size() - 1);
		} else {
			return new Sprite();
		}
	}
	
	public static void free(Sprite sprite) {
		if(sprite != null) {
			freeSprites.add(sprite);
		} else {
			Log.w("Cave", "NULL was passed to free(Sprite)");
		}
	}
	
}
